let pomelo = window.pomelo;
let connHost;   //connector地址
let connPort;   //connector端口
let isConnectorEntry=false;    //是否连接Connector
let testUid = "20026";    //测试用户

let ownBoxLen = 0;   //已拥有宝箱数量
let waitBoxLen = 0;    //待获取宝箱数量
let nowCountDownId = "";    //当前倒计时宝箱ID

//用户登陆
async function login(elem) {
  let route = "login.loginHandler.login";

  if(isConnectorEntry){   //重新登陆
    pomelo.disconnect();
    isConnectorEntry = false;
  }

  if(!isConnectorEntry)   //初始连接
    await connectorEntry(document.getElementById("connBtn"));

  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.request(route, {} , async data => {
      if(data.code !== 200) return;
      showLog("用户登陆成功");
      console.log(data);
      elem.setAttribute("value", "重新登陆");
      resolve();
    });
  });
}

//打开宝箱页面
function openBoxPage(elem){
  let route = "box.boxHandler.openBoxPage";
  return new Promise(()=>{
    pomelo.request(route, {} , data => {
      showLog("打开宝箱页面");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      // ownBoxLen, waitOpenBoxLen, nowCountDownBox
      // 已拥有宝箱数量
      ownBoxLen = data.ownBoxLen;
      // 待领取宝箱数量
      waitBoxLen = data.waitOpenBoxLen
      // 当前倒计时宝箱ID
      nowCountDownId = data.nowCountDownBox && data.nowCountDownBox.nowCountDownId;
    });
  });
}

//看视频加速
function cleanCountDown(elem){
  let route = "box.boxHandler.clearCountDown";
  return new Promise(()=>{
    pomelo.request(route, { id: nowCountDownId } , data => {
      showLog("看视频加速"+nowCountDownId);
      showLog(JSON.stringify(data));
    });
  });
}

//打开宝箱
function openBox(elem){
  let route = "box.boxHandler.openBox";
  return new Promise(()=>{
    pomelo.request(route, {} , data => {
      showLog("打开宝箱");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
      waitBoxLen = data.openBoxLen;
    });
  });
}

//普通领取
function obtainPrize(elem){
  let route = "box.boxHandler.obtainPrize";
  return new Promise(()=>{
    pomelo.request(route, {} , data => {
      showLog("普通领取宝箱");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
    });
  });
}

//双倍领取
function doubleObtainPrize(elem){
  let route = "box.boxHandler.doubleObtainPrize";
  return new Promise(()=>{
    pomelo.request(route, {} , data => {
      showLog("双倍领取宝箱");
      showLog(JSON.stringify(data));
      if(data.code !== 200) return;
    });
  });
}

//监听pomelo事件
window.onload = function(){
  //宝箱数量增加
  pomelo.on("s2c_chest_increase",function(msg){
    ownBoxLen++;
    showLog("宝箱数量增加");
  });

  //宝箱开始倒计时
  pomelo.on("s2c_chest_start_count_down",function(msg){
    nowCountDownId = msg.id;
    showLog(nowCountDownId +"宝箱开始倒计时");
  });

  //宝箱结束倒计时
  pomelo.on("s2c_count_down_end",function(msg){
    nowCountDownId = "";
    ownBoxLen++;
    showLog("宝箱结束倒计时");
  });
}

//-----------------------------不重要的--------------

//连接Connector
async function connectorEntry(elem) {
  let route = "connector.entryHandler.entry";
  if(!connHost || !connPort) 
    await gateConnector(document.getElementById("gateBtn"));
  //首先初始化connector地址
  return new Promise( resolve => {
    pomelo.init({ host: connHost, port: connPort, log: true }, function() {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("初始化connector成功");
        elem.setAttribute("disabled","");
        isConnectorEntry = true;
        resolve();
      });
    });
  });
}

//Gate获取Connector端口地址
function gateConnector(elem) {
  let route = "gate.gateHandler.entry";
  let gateHost = "127.0.0.1";
  let gatePort = 3110;
  initNowUid();   //设置用户账号
  return new Promise( resolve => {
    pomelo.init({ host: gateHost, port: gatePort, log: true }, function(err) {
      pomelo.request(route, { uid: testUid } , data => {
        showLog("Gate获取Connector端口地址");
        showLog(JSON.stringify(data));
        if(!data || data.code != 200) return;
        elem.setAttribute("disabled","");
        connHost = data.host;
        connPort = data.port;
        resolve();
      });
    });
  });
}

//显示宝箱信息
function showBoxInfo(){
  showLog("已拥有宝箱: "+ownBoxAry.length+"; 待收取宝箱: "+waitBoxAry.length);

  console.log("已拥有宝箱详情");
  console.log(ownBoxAry);
  console.log("待收取宝箱详情");
  console.log(waitBoxAry);
}

//获取用户选中的uid
function initNowUid(){
  let userName = document.querySelector("input[name='uid']:checked");
  testUid = userName.value;
}

//显示日志
function showLog(log){
  let logPanel = document.getElementById("showLog");
  let textobj=document.createTextNode(log+"\n");
  logPanel.prepend(textobj);
}
